﻿''' <summary>
''' Saves the cursor position and colors of the console when created and restores them when disposed.
''' </summary>
Public Class ConsoleStateSave
    Implements IDisposable

    Public ReadOnly CursorLeft As Integer
    Public ReadOnly CursorTop As Integer
    Public ReadOnly ForeColor As ConsoleColor
    Public ReadOnly BackColor As ConsoleColor

    Public Sub New()
        CursorLeft = Console.CursorLeft
        CursorTop = Console.CursorTop
        ForeColor = Console.ForegroundColor
        BackColor = Console.BackgroundColor
    End Sub

    ''' <summary>
    ''' Resets the console cursor position and colors to what their values where when this instance was created.
    ''' </summary>
    Public Sub Reset()
        Console.CursorLeft = CursorLeft
        Console.CursorTop = CursorTop
        Console.ForegroundColor = ForeColor
        Console.BackgroundColor = BackColor
    End Sub

    Private disposedValue As Boolean
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                Reset()
            End If
        End If
        disposedValue = True
    End Sub
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
    End Sub
End Class
